package com.atguigu.spzx.cart.controller;

import com.spzx.cart.domain.CartInfo;
import com.atguigu.spzx.cart.service.CartService;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.security.annotation.InnerAuth;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping
@Tag(name = "购物车模块")
public class CartController extends BaseController {
    @Resource
    CartService cartService;
    @InnerAuth
    @Operation(summary = "内部调用接口：结算时获取用户购物车中选中的购物项")
    @GetMapping("/getCartInfos/{userId}")
    public R<List<CartInfo>> getCartInfos(@PathVariable("userId")Long userId ){
        List<CartInfo> cartInfos = cartService.getCartInfos(userId);
        return R.ok(cartInfos);
    }
    ///cart/addToCart/5/1
    @Operation(summary = "添加商品到购物车")
    @GetMapping("/addToCart/{skuId}/{skuNum}")
    public AjaxResult addToCart(@PathVariable("skuId")Long skuId,
                                @PathVariable("skuNum")Integer skuNum ){
        cartService.add2Cart(skuId,skuNum);
        return success();
    }
    @Operation(summary = "查询购物车")
    @GetMapping("/cartList")
    public AjaxResult cartList(){
        List<CartInfo> cartInfos = cartService.selectCartList();
        return success(cartInfos);
    }
    ///deleteCart/12
    @Operation(summary = "删除购物项")
    @DeleteMapping("/deleteCart/{skuId}")
    public AjaxResult deleteCart(@PathVariable("skuId")Long skuId){
        cartService.deleteCart(skuId);
        return success();
    }
    //get  /checkCart/11/0  : 修改指定购物项的选中状态
    @Operation(summary = "修改指定购物项的选中状态")
    @GetMapping("/checkCart/{skuId}/{status}")
    public AjaxResult checkCart(@PathVariable("skuId")Long skuId,
                                @PathVariable("status")Integer status){
        cartService.checkCart(skuId,status);
        return success();
    }
    //get /cart/allCheckCart/0  :修改所有购物项的选中状态
    @Operation(summary = "修改所有购物项的选中状态")
    @GetMapping("/allCheckCart/{status}")
    public AjaxResult allCheckCart(@PathVariable("status")Integer status){
        cartService.allCheckCart(status);
        return success();
    }
    //get /cart/clearCart  :删除购物车/清空购物车
    @Operation(summary = "删除购物车")
    @GetMapping("/clearCart")
    public AjaxResult clearCart(){
        cartService.clearCart();
        return success();
    }
}
